package com.zkdbchange.dbchange;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSON;
import com.zkdbchange.dbchange.config.DBProperty;
import com.zkdbchange.dbchange.config.SpringContextHolder;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DbchangeApplication {

    static CuratorFramework curatorFramework;
    static {
         curatorFramework = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1")
                .sessionTimeoutMs(5000)
                .retryPolicy(new ExponentialBackoffRetry(1000, 10)).build();
        curatorFramework.start();
    }

    public static void main(String[] args) {

        SpringApplication.run(DbchangeApplication.class, args);
            pathChildrenCache();

    }

    private static String path="/register";

    private static void pathChildrenCache(){
        PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework, path, true);
        PathChildrenCacheListener pathChildrenCacheListener = new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                PathChildrenCacheEvent.Type type = event.getType();
                if (PathChildrenCacheEvent.Type.CHILD_UPDATED.equals(type)) {
                    //监听添加
                    System.out.println("add");
                    DruidDataSource druidDataSource= (DruidDataSource) SpringContextHolder.getBean("dataSource");;
                    byte[] bytes = curatorFramework.getData().forPath(path+"/dbsource");

                    DBProperty parse = JSON.parseObject(bytes, DBProperty.class);
                    DruidDataSource temDataSource = findDataSource(parse,druidDataSource);
                    temDataSource.restart();
                }
            }
        };
        pathChildrenCache.getListenable().addListener(pathChildrenCacheListener);
        try {
            pathChildrenCache.start();
        } catch (Exception e) {
            throw new RuntimeException("监听节点变化异常！", e);
        }
    }

    private static DruidDataSource findDataSource(DBProperty dbProperty,DruidDataSource datasource){

        datasource.setUrl(dbProperty.getDatasourceUrl());
        datasource.setUsername(dbProperty.getDatasourceUserName());
        datasource.setPassword(dbProperty.getDatasourcePassword());
        datasource.setDriverClassName(dbProperty.getDriverClassName());
        datasource.setConnectionProperties(dbProperty.getConnectionProperties());
        return datasource;
    }
}
